#include<bits/stdc++.h>
using namespace std;

int T, N;
int a[200005];
// long long dp[2005][2][2005];
long long dp[200005][2][2]; // 1:col[i] = col[i - 1]; 0: col[i] != col[i - 1]
long long sum[200005];
vector<int> numpos[1000006];
int le[1000006];

int main(){
	freopen("color.in", "r", stdin);
	freopen("color.out", "w", stdout);
	
	scanf("%d", &T);
	while (T --){
		scanf("%d", &N);
		memset(dp, 0, sizeof(dp));
		for (int i = 0; i <= 1000000; i ++){
			numpos[i].clear();
			le[i] = 0;
		}
		a[0] = -114514, sum[0] = 0;
		for (int i = 1; i <= N; i ++){
			scanf("%d", &a[i]);
			sum[i] = sum[i - 1] + (a[i] == a[i - 1]) * a[i];
			numpos[a[i]].push_back(i);
			// cout << i << " " << sum[i] << ";;\n";
		}
		for (int i = 1; i <= N; i ++){
			le[a[i]] ++;
			// red(0)
			dp[i][0][1] = max(dp[i - 1][0][1], dp[i - 1][0][0]) + (a[i - 1] == a[i]) * a[i];
			// dp[i][0][0]
			dp[i][0][0] = max(dp[i - 1][1][0], dp[i - 1][1][1]);
			if (le[a[i]] > 1){
				int pos = numpos[a[i]][le[a[i]] - 2];
				dp[i][0][0] = max(dp[pos + 1][1][0], dp[pos + 1][1][1]) + (sum[i - 1] - sum[pos + 1]) + a[i];
			}
			/*
			for (int j = 1; j < i; j ++){
				long long tmp = max(dp[j][1][0], dp[j][1][1]) + (sum[i - 1] - sum[j])
												 + (a[i] == a[j - 1]) * a[i];
				dp[i][0][0] = max(dp[i][0][0], tmp);
			}
			*/
			// blue(1)
			dp[i][1][1] = max(dp[i - 1][1][1], dp[i - 1][1][0]) + (a[i - 1] == a[i]) * a[i];
			// dp[i][1][0]
			dp[i][1][0] = max(dp[i - 1][0][0], dp[i - 1][0][1]);
			if (le[a[i]] > 1){
				int pos = numpos[a[i]][le[a[i]] - 2];
				dp[i][1][0] = max(dp[pos + 1][0][0], dp[pos + 1][0][1]) + (sum[i - 1] - sum[pos + 1]) + a[i];
			}
			/*
			for (int j = 1; j < i; j ++){
				long long tmp = max(dp[j][0][0], dp[j][0][1]) + (sum[i - 1] - sum[j])
												 + (a[i] == a[j - 1]) * a[i];
				dp[i][1][0] = max(dp[i][1][0], tmp);
			}
			*/
		}
		long long ans = 0;
		for (int i = 0; i < 2; i ++){
			for (int j = 0; j < 2; j ++){
				ans = max(ans, dp[N][i][j]);
			}
		}
		printf("%lld\n", ans);
	}

	return 0;
}
